package com.wing.purchase.service;

import com.alibaba.fastjson.JSON;
import com.wing.auth.service.AuthService;
import com.wing.common.utils.JsonResult;
import com.wing.purchase.entity.request.PurchaseCreateDtoRequest;
import com.wing.purchase.entity.request.PurchaseCreateRequest;
import com.wing.purchase.entity.request.RdrecordDetailsRequest;
import com.wing.redis.utils.redis.RedisHelperImpl;
import com.wing.set.entity.ChanjetSet;
import com.wing.set.service.ChanjetSetService;
import com.wing.util.ConstantsUtil;
import com.wing.util.HttpsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 仓库管理
 */
@Service
@Slf4j
public class PurchaseServiceImpl implements PurchaseService {

    @Resource
    RedisHelperImpl redisHelper;

    @Resource
    AuthService authService;

    @Resource
    ChanjetSetService chanjetSetService;


    @Override
    public JsonResult syncAddPurchaseWork(PurchaseCreateDtoRequest purchaseCreateRequest) {
        ChanjetSet chanjetSet = chanjetSetService.getById(1L);

        Map<String, String> headers = new HashMap<>();
        headers.put("appKey", chanjetSet.getAppKey());
        headers.put("appSecret", chanjetSet.getAppSecret());
        if(redisHelper.getValue("chanjetToken") == null){
            authService.getToken();
        }
        headers.put("openToken", redisHelper.getValue("chanjetToken").toString());
        // 创建最外层map，包含dto
   /*     Map<String, Object> root = new HashMap<>();
        root.put("dto", purchaseCreateRequest);*/

        List<RdrecordDetailsRequest> res = purchaseCreateRequest.getDto().getRes();

        PurchaseCreateRequest dto = purchaseCreateRequest.getDto();
        dto.setRDRecordDetails(res);
        log.info("----------------------"+JSON.toJSONString(purchaseCreateRequest));
        String result = HttpsUtils.httpsPostByJson(ConstantsUtil.CREATE_PURCHASE_RECEIVE, headers, JSON.toJSONString(purchaseCreateRequest));

        return JsonResult.success(result);

    }



}
